
<!--用户管理界面。应当注意修改路由配置，让首页可以重定向至此-->
<template>
  <div>

<!--    顶部搜索栏起-->
    <el-card id="search">
      <el-input v-model="searchModel.userName" placeholder="请输入用户名" />
      <el-input v-model="searchModel.phone" placeholder="请输入手机号" />
      <el-button type="success">查询</el-button>

<!--      当前用户为超级管理员时，显示新增管理员以及切换用户页面按钮，由变量superAdmin控制-->
      <el-button v-if="superAdmin && switchName==='管理员'" type="primary" @click="openEditUI(null, null)">新增</el-button>

<!--      switchName为当前页面显示的用户类型，点击即可切换，逻辑实现在switching函数中-->
      <el-button v-if="superAdmin" type="primary" style="float: right" @click="switching">{{ switchName }}</el-button>
    </el-card>
    <!--    顶部搜索栏止-->

    <el-card style="margin-top: 30px">
      <!--      用户表格-->
      <el-table v-if="switchName==='用户'" :data="userList" style="width: 100%">
        <el-table-column prop="No" label="序号" width="100" align="center" header-align="center" />
        <el-table-column prop="sno" label="学号/职工号" width="150" align="center" header-align="center" />
        <el-table-column prop="name" label="姓名" width="150" align="center" header-align="center" />
        <el-table-column prop="id" label="用户名" width="180" align="center" header-align="center" />
        <el-table-column prop="pwd" label="密码" width="150" align="center" header-align="center" />
        <el-table-column prop="college" label="学院" width="210" align="center" header-align="center" />
        <el-table-column label="操作" width="150" align="center" header-align="center">
          <template  #default="scope">
            <el-button type="default" size="small" @click="openEditUI(scope.row)">修改</el-button>
            <el-button type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      管理员表格-->
      <el-table v-if="switchName==='管理员'" :data="adminList" style="width: 100%">
        <el-table-column prop="No" label="序号" width="auto" align="center" header-align="center" />
        <el-table-column prop="name" label="姓名" width="auto" align="center" header-align="center" />
        <el-table-column prop="id" label="用户名" width="auto" align="center" header-align="center" />
        <el-table-column prop="college" label="学院" width="auto" align="center" header-align="center" />
        <el-table-column prop="role" label="角色" width="auto" align="center" header-align="center" />
        <el-table-column label="操作" width="auto" align="center" header-align="center" fixed="right">
          <template  #default="scope">
              <el-button type="default" size="small" @click="openEditUI(scope.row, '授权')">授权</el-button>
              <el-button type="default" size="small" @click="openEditUI(scope.row, '修改')">修改</el-button>
              <el-button type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      分页器和页码选择器-->
      <el-pagination
        :current-page="searchModel.pageNo"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!--    弹窗，弹窗中显示的内容动态改变，改变的逻辑在openEditUI函数中。调用openEditUI即可显示弹窗-->
    <el-dialog :visible.sync="dialogFormVisible" :title="title" center @close="closeEditUI">
      <el-form :model="userForm">
        <el-form-item label="姓名" label-width="100px" v-if="title === '修改用户' || title === '修改管理员' || title === '新增管理员'">
          <el-input v-model="userForm.name" autocomplete="off"  placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="用户名" label-width="100px" v-if="title==='修改用户' || title === '修改管理员' || title === '新增管理员'">
          <el-input v-model="userForm.id" autocomplete="off"  placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="密码" label-width="100px" v-if="title==='修改用户' || title === '新增管理员'">
          <el-input v-model="userForm.pwd" autocomplete="off"  placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="学院" label-width="100px" v-if="title==='修改用户' || title === '修改管理员' || title === '新增管理员'">
          <el-input v-model="userForm.college" autocomplete="off"  placeholder="请输入学院名称"/>
        </el-form-item>
<!--        用于角色选择的复选框-->
        <el-form-item label="角色" label-width="100px" v-if="title === '修改管理员权限' || title === '新增管理员'">
          <template>
            <el-scrollbar>
              <div style="height: 200px">
                <el-checkbox
                  v-model="checkAll"
                  :indeterminate="isIndeterminate"
                  @change="handleCheckAllChange"
                >校级管理员</el-checkbox
                >
                <el-checkbox-group
                  v-model="checkedRoles"
                  @change="handleCheckedRolesChange"
                  style="display: flex; flex-direction: column; margin-left: 10px; margin-top: 5px;"
                >
                  <el-checkbox v-for="role in roles" :key="role" :label="role">{{
                      role
                    }}</el-checkbox>
                </el-checkbox-group>
              </div>
            </el-scrollbar>
          </template>
        </el-form-item>
      </el-form>
<!-- 弹窗底部两个按钮-->
        <template #footer>
          <span  class="dialog-footer">
          <el-button type="primary" @click="dialogFormVisible = false" >
            确定
          </el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </span>
        </template>

    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'User',
  data() {
    return {
      title: '新增用户',
      dialogFormVisible: false,

      superAdmin: true,

      switchName: '用户',

      total: 0,
      searchModel: {
        userName: '',
        phone: '',
        pageNo: 1,
        pageSize: 10
      },
      // 用户列表，用于存储普通用户的信息
      userList: [
        {
          No: 1,
          sno: '2018000001',
          name: '张三',
          id: 'zs',
          pwd: '123456',
          college: '计算机与软件学院'
        }, {
          No: 2,
          sno: '2018000001',
          name: '张三',
          id: 'zs',
          pwd: '123456',
          college: '计算机与软件学院'
        }
      ],
      // 管理员列表，用于存储管理员的信息
      adminList: [
        {
          No: 1,
          name: '李四',
          id: 'ls',
          college: '计算机与软件学院',
          role: '校级管理员'
        }
      ],
      // 临时存储用户/管理员信息，同时也是弹窗中输入框的值。该对象应为普通用户与管理员属性的并集
      userForm: {
        No: '',
        sno: '',
        name: '',
        id: '',
        pwd: '',
        college: '',
        role: ''
      },

      checkAll: false,
      isIndeterminate: false,
      checkedRoles: [],
      roles: ['计算机与软件学院管理员', '。。。', '1', '2', '3', '4', '5']

    }
  },
  methods: {
    // 用于处理分页器的改变
    handleSizeChange(val) {

    },
    handleCurrentChange(val) {

    },

    // 切换用户与管理员
    switching() {
      if (this.switchName === '用户') {
        this.switchName = '管理员'
      } else {
        this.switchName = '用户'
      }
    },

    // 弹窗初始化
    openEditUI(val, type) {
      if (this.switchName === '用户') {
        if (type !== null) {
          this.title = '修改用户'
          this.userForm.id = val.id
          this.userForm.name = val.name
          this.userForm.pwd = val.pwd
          this.userForm.college = val.college
        }
      } else if (type === '授权') {
        this.title = '修改管理员权限'
      } else if (this.switchName === '管理员') {
        if (type != null) {
          this.title = '修改管理员'
          this.userForm.id = val.id
          this.userForm.name = val.name
          this.userForm.pwd = val.pwd
          this.userForm.college = val.college
        } else {
          this.title = '新增管理员'
        }
      }
      this.dialogFormVisible = true
    },

    // 弹窗关闭后的操作
    closeEditUI() {
      this.dialogFormVisible = false
      this.userForm = {
        No: '',
        sno: '',
        name: '',
        id: '',
        pwd: '',
        college: '',
        role: ''
      }
    },

    // 处理复选框的改变
    handleCheckAllChange(val) {
      this.checkedRoles = val ? this.roles : []
      this.isIndeterminate = false
    },
    handleCheckedRolesChange(value) {
      let checkedCount = value.length
      this.checkAll = checkedCount === this.roles.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.roles.length
    }
  }
}
</script>

<style scoped>
  #search .el-input{
    width: 200px;
    margin-right: 20px;
  }
  .dialog-footer .el-button{
    margin-right: 10px;
  }
  .el-dialog .el-input{
    width: 80%;
  }
</style>
